package org.frv.wispr.web.account;

import org.apache.wicket.Component;
import org.apache.wicket.util.value.ValueMap;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.behavior.IBehavior;
import org.frv.wispr.WisprApplication;

/**
 * Created by IntelliJ IDEA.
 * User: frank
 * Date: Jan 12, 2008
 * Time: 11:49:04 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class WisprLoginPanel extends Panel {
    public WisprLoginPanel(String id) {
        super(id);
        add(new SignInForm("signinForm")).setOutputMarkupId(false);
    }

    public abstract boolean signIn(String username, String password);

    /**
     * Sign in form.
     */
    public final class SignInForm extends StatelessForm {
        private static final long serialVersionUID = 1L;
        private boolean rememberMe = true; //currently only remembers username. we need to add logic for cookie

        /**
         * Constructor.
         *
         * @param id id of the form component
         */
        public SignInForm(final String id) {
            super(id, new CompoundPropertyModel(new ValueMap()));

            // only remember username, not passwords
            add(new TextField("username").setPersistent(rememberMe).setOutputMarkupId(false));
            add(new PasswordTextField("password").setOutputMarkupId(false));
            add(new CheckBox("rememberMe", new PropertyModel(this, "rememberMe")));
        }

        /**
         * @see org.apache.wicket.Component#getMarkupId()
         */
        public String getMarkupId() {
            // fix javascript id
            return getId();
        }

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        public final void onSubmit() {
            if (!rememberMe) {
                // delete persistent data
                getPage().removePersistedFormData(SignInForm.class, true);
            }

            ValueMap values = (ValueMap) getModelObject();
            String username = values.getString("username");
            String password = values.getString("password");

            if (signIn(username, password)) {
                // continue or homepage?
                if (!getPage().continueToOriginalDestination()) {
                    setResponsePage(WisprApplication.get().getHomePage());
                }
            } else {
                // Try the component based localizer first. If not found try the
                // application localizer. Else use the default
                error(getLocalizer().getString("exception.login", this, "Illegal username password combo"));
            }
        }

        /**
         * @return true if formdata should be made persistent (cookie) for later
         *         logins.
         */
        public boolean getRememberMe() {
            return rememberMe;
        }

        /**
         * Remember form values for later logins?.
         *
         * @param rememberMe true if formdata should be remembered
         */
        public void setRememberMe(boolean rememberMe) {
            this.rememberMe = rememberMe;
            ((FormComponent) get("username")).setPersistent(rememberMe);
		}
	}

}
